<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class ReferenceMaterial extends Model
{
    use HasFactory;

    protected $fillable = [
        'type_id',
        'user_id',
        'project_id',
        'title',
        'brand',
        'specifications',
        'unit',
        'price',
        'num',
        'total',
        'tax_rate',
        'tax',
        'tax_price',
        'freight',
        'installation',
        'address',
        'material_supplier',
        'contact',
        'contact_mobile',
        'grade',
        'signing_date',
        'remarks',

    ];

    const POSTONLY = [
        'type_id',
        'user_id',
        'project_id',
        'title',
        'brand',
        'specifications',
        'unit',
        'price',
        'num',
        'total',
        'tax_rate',
        'tax',
        'tax_price',
        'freight',
        'installation',
        'address',
        'material_supplier',
        'contact',
        'contact_mobile',
        'grade',
        'signing_date',
        'remarks',
    ];


    protected function serializeDate(\DateTimeInterface $date)
    {
        return $date->format($this->dateFormat ?: 'Y-m-d H:i:s');
    }

    public function project()
    {
        return $this->hasOne(Project::class,'id','project_id')->select('id','title','status');
    }


    public function user()
    {
        return $this->hasOne(User::class,'id','user_id')->select('id','name');
    }

    public function application()
    {
        return $this->hasOne(Application::class,'source_id','id')->where('model','MaterialContract')->orderBy('id','desc');
    }

}
